package com.basic.library.utils;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.internal.$Gson$Types;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class LoginUtil {

    public static String getToken() {
        return LocalData.getInstance().getUserInfo().getToken();
//		return PreferencesUtils.getStringPreferences("token", "token", "");
    }

    public static boolean isLogin() {
        return LocalData.getInstance().isLogin();
    }

    //文件最后修改时间
    public static void saveLastModified(String fileName, String lastModified) {
        PreferencesUtils.setStringPreferences(fileName, fileName, lastModified);
    }

    public static String getLastModified(String fileName) {
        return PreferencesUtils.getStringPreferences(fileName, fileName, "");
    }

    //保存下载文件路径
    public static void saveFilePath(String fileName, String filePath) {
        PreferencesUtils.setStringPreferences(fileName, fileName, filePath);
    }

    public static String getFilePath(String fileName) {
        return PreferencesUtils.getStringPreferences(fileName, fileName, "");
    }

    //保存数据
    public static void saveData(String dataName, String data) {
        PreferencesUtils.setStringPreferences(dataName, dataName, data);
    }

    public static String getData(String dataName) {
        return PreferencesUtils.getStringPreferences(dataName, dataName, "");
    }

    public static void saveObj(Object obj, Object o) {
        String dataName = obj.getClass().getName();
        if (obj instanceof String)
            dataName = obj.toString();
        PreferencesUtils.setStringPreferences(dataName, dataName, new Gson().toJson(o));
    }

    public static <T> T getObj(Object obj, ResultCallback<T> resultCallback) {
        T t = null;
        String dataName = obj.getClass().getName();
        if (obj instanceof String)
            dataName = obj.toString();
        String json = PreferencesUtils.getStringPreferences(dataName, dataName, "");
        try {
            t = new Gson().fromJson(json, resultCallback.mType);
//            resultCallback.onResponse(t);
        } catch (JsonSyntaxException e) {
            t = null;
        }
        return t;
    }

    //推送id
    public static void saveRegId(String regId) {
        PreferencesUtils.setStringPreferences("regId", "regId", regId);
    }

    public static String getRegId() {
        return PreferencesUtils.getStringPreferences("regId", "regId", "");
    }

    private static LoginUtil loginUtil = null;

    public static synchronized LoginUtil getInstance() {
        if (loginUtil == null) {
            loginUtil = new LoginUtil();
        }
        return loginUtil;
    }

    public abstract static class ResultCallback<T> {
        Type mType;

        public ResultCallback() {
            mType = getSuperclassTypeParameter(getClass());
        }

        static Type getSuperclassTypeParameter(Class<?> subclass) {
            Type superclass = subclass.getGenericSuperclass();
            if (superclass instanceof Class) {
                throw new RuntimeException("Missing type parameter.");
            }
            ParameterizedType parameterized = (ParameterizedType) superclass;
            return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
        }

//        public abstract void onResponse(T response);
    }
}
